$(document).ready(function () {
    //期数选择初始化
    kjCommonFun.pageAndTerm();
    //开奖数据类型7星彩
    kjCommonFun.ltType = '04';
    kjCommonFun.ltName = 'qxc';
    //回调方法
    kjCommonFun.callBackFun = qxcShowData;
    //获取页面初始参数，并调用数据
    kjCommonFun.iniParam();

});

//开奖数据显示方法
function qxcShowData(data) {
    if (data == undefined || data.list == undefined || data.length == 0 || data.list.length == 0) {
        var obj = $(".m-historyTab").find('tr').eq(0).find('th');
        var len = 0;
        obj.each(function () {
            var colspanNum = $(this).attr('colspan');
            if (colspanNum == undefined) {
                len++;
            } else {
                len = len + parseInt(colspanNum);
            }
        })
        $("#historyData").html('<tr><td colspan="' + len + '"  onmouseover="kjCommonFun.delOnmouseOverBg(this)">' + kjCommonFun.NoDataStr + '</td></tr>');
        $('.m-page').html('');
        kjCommonFun.getIframeHeight($('.g-bd').height());
        return;
    }
    var hisData = data.list;
    var dataStr = '',
        levList = '',
        lev1 = [],
        lev2 = [],
        lev3 = [],
        lev4 = [],
        lev5 = [],
        lev6 = [];
    var drawList, numberCode;

    var rowspanStr = " rowspan='1'";
    var awardInfo = Array();

    var prizeLevel = ['一等奖', '二等奖', '三等奖', '四等奖', '五等奖', '六等奖'];

    for (var i = 0, len = hisData.length; i < len; i++) {
        var flag = hisData[i].lotteryPromotionFlag;
        levList = hisData[i].prizeLevelList;
        // if(i== 0){
        //     levList =[];
        // }
        var clsName1 = 'u-7xc',
            clsName2 = 'u-7xc lineb2Rt';
        lev1.piece = lev1.money = lev2.piece = lev2.money = lev3.piece = lev3.money = '';
        lev4.piece = lev4.money = lev5.piece = lev5.money = lev6.piece = lev6.money = '';
        awardInfo=[];
        for (var z = 0; z < prizeLevel.length; z++) {
            for (var j = 0; j < levList.length; j++) {
                if (levList[j].prizeLevel != undefined) {
                    var pos = levList[j].prizeLevel.indexOf(prizeLevel[z]);
                    if (pos > -1) {
                        if (awardInfo[z] == undefined) {
                            awardInfo[z] = [];
                        }
                        awardInfo[z].push(levList[j]);
                    }
                }
            }
        }

        if (flag == 1) {
            rowspanStr = " rowspan='3'";
            var defultLev = '<td class="bgecf5fe lineb2Rt" colspan="2">已结束</td>';
            var lev1Str = lev2Str = lev3Str = lev4Str = lev5Str = lev6Str = defultLev;
            if(levList.length == 0){
                // 未开奖
                lev1.piece ="--";
                lev1.money ="--";
                lev2.piece ="--";
                lev2.money ="--";
                lev3.piece ="--";
                lev3.money ="--";
                lev4.piece ="--";
                lev4.money ="--";
                lev5.piece ="--";
                lev5.money ="--";
                lev6.piece ="--";
                lev6.money ="--";
                lev1Str = '<td class="bgecf5fe u-txar">--</td>' +
                          '<td class="u-txar bgecf5fe lineb2Rt u-txar">--</td>';
                lev2Str = '<td class="bgecf5fe u-txar">--</td>' +
                           '<td class="bgecf5fe lineb2Rt u-txar">--</td>';
                lev3Str = '<td class="bgecf5fe u-txar">--</td>' +
                          '<td class="bgecf5fe lineb2Rt">--</td>'
                lev4Str = '<td class="bgecf5fe u-txar">--</td>' +
                          '<td class="bgecf5fe lineb2Rt">--</td>'
                lev5Str = '<td class="bgecf5fe u-txar">--</td>' +
                          '<td class="bgecf5fe lineb2Rt">--</td>'
                lev6Str = '<td class="bgecf5fe u-txar">--</td>' +
                          '<td class="bgecf5fe lineb2Rt">--</td>'        
                        
            }else{
                // 已开奖
                awardInfo.forEach(function (info, index) {
                    switch (index) {
                        case 0:
                            lev1.piece = info[0].stakeCount;
                            lev1.money = info[0].stakeAmount;
                            if (info.length > 1) {
                                lev1Str = '<td class="bgecf5fe u-txar">' + kjCommonFun.kjCheckNull(info[1].stakeCount) + '</td>' +
                                    '<td class="u-txar bgecf5fe lineb2Rt">' + kjCommonFun.kjCheckNull(info[1].stakeAmount) + '</td>'
                            }
                            break;
                        case 1:
                            lev2.piece = info[0].stakeCount;
                            lev2.money = info[0].stakeAmount;
                            if (info.length > 1) {
                                lev2Str = '<td class="bgecf5fe u-txar">' + kjCommonFun.kjCheckNull(info[1].stakeCount) + '</td>' +
                                    '<td class="bgecf5fe lineb2Rt u-txar">' + kjCommonFun.kjCheckNull(info[1].stakeAmount) + '</td>'
                            }
                            break;
                        case 2:
                            lev3.piece = info[0].stakeCount;
                            lev3.money = info[0].stakeAmount;
                            if (info.length > 1) {
                                lev3Str = '<td class="bgecf5fe u-txar">' + kjCommonFun.kjCheckNull(info[1].stakeCount) + '</td>' +
                                    '<td class="bgecf5fe lineb2Rt">' + kjCommonFun.kjCheckNull(info[1].stakeAmount) + '</td>'
                            }
                            break;
                        case 3:
                            lev4.piece = info[0].stakeCount;
                            lev4.money = info[0].stakeAmount;
                            if (info.length > 1) {
                                lev4Str = '<td class="bgecf5fe u-txar">' + kjCommonFun.kjCheckNull(info[1].stakeCount) + '</td>' +
                                    '<td class="bgecf5fe lineb2Rt">' + kjCommonFun.kjCheckNull(info[1].stakeAmount) + '</td>'
                            }
                            break;
                        case 4:
                            lev5.piece = info[0].stakeCount;
                            lev5.money = info[0].stakeAmount;
                            if (info.length > 1) {
                                lev5Str = '<td class="bgecf5fe u-txar">' + kjCommonFun.kjCheckNull(info[1].stakeCount) + '</td>' +
                                    '<td class="bgecf5fe lineb2Rt">' + kjCommonFun.kjCheckNull(info[1].stakeAmount) + '</td>'
                            }
                            break;
                        case 5:
                            lev6.piece = info[0].stakeCount;
                            lev6.money = info[0].stakeAmount;
                            if (info.length > 1) {
                                lev6Str = '<td class="bgecf5fe u-txar">' + kjCommonFun.kjCheckNull(info[1].stakeCount) + '</td>' +
                                    '<td class="bgecf5fe lineb2Rt">' + kjCommonFun.kjCheckNull(info[1].stakeAmount) + '</td>'
                            }
                            break;
                    }
                })
            }
        } else {
            rowspanStr = " rowspan='1'";
            if(levList.length == 0){
                // 未开奖
                lev1.piece ="--";
                lev1.money ="--";
                lev2.piece ="--";
                lev2.money ="--";
                lev3.piece ="--";
                lev3.money ="--";
                lev4.piece ="--";
                lev4.money ="--";
                lev5.piece ="--";
                lev5.money ="--";
                lev6.piece ="--";
                lev6.money ="--";
                lev1Str = '<td class="bgecf5fe u-txar">--</td>' +
                          '<td class="u-txar bgecf5fe lineb2Rt">--</td>';
                lev2Str = '<td class="bgecf5fe u-txar">--</td>' +
                           '<td class="bgecf5fe lineb2Rt u-txar">--</td>';
                lev3Str = '<td class="bgecf5fe u-txar">--</td>' +
                          '<td class="bgecf5fe lineb2Rt">--</td>'
                lev4Str = '<td class="bgecf5fe u-txar">--</td>' +
                          '<td class="bgecf5fe lineb2Rt">--</td>'
                lev5Str = '<td class="bgecf5fe u-txar">--</td>' +
                          '<td class="bgecf5fe lineb2Rt">-</td>'
                lev6Str = '<td class="bgecf5fe u-txar">--</td>' +
                          '<td class="bgecf5fe lineb2Rt">--</td>'        
                        
            }
            for (var j = 0; j < levList.length; j++) {
                if (levList[j].prizeLevel == '一等奖') {
                    lev1.piece = levList[j].stakeCount;
                    lev1.money = levList[j].stakeAmount;
                }
                if (levList[j].prizeLevel == '二等奖') {
                    lev2.piece = levList[j].stakeCount;
                    lev2.money = levList[j].stakeAmount;
                }
                if (levList[j].prizeLevel == '三等奖') {
                    lev3.piece = levList[j].stakeCount;
                    lev3.money = levList[j].stakeAmount;
                }
                if (levList[j].prizeLevel == '四等奖') {
                    lev4.piece = levList[j].stakeCount;
                    lev4.money = levList[j].stakeAmount;
                }
                if (levList[j].prizeLevel == '五等奖') {
                    lev5.piece = levList[j].stakeCount;
                    lev5.money = levList[j].stakeAmount;
                }
                if (levList[j].prizeLevel == '六等奖') {
                    lev6.piece = levList[j].stakeCount;
                    lev6.money = levList[j].stakeAmount;
                }
            }
        }

        numberCode = hisData[i].lotteryDrawResult.split(' ');
        //暂定期号
        if (parseInt(hisData[i].ruleType) == 1) {
            clsName1 = 'u-7xc-n';
            clsName2 = 'u-7xc-n lineb2Rt last'
        }
        dataStr += '<tr><td ' + rowspanStr + '>' + hisData[i].lotteryDrawNum + '</td>' +
            '<td class="lineb2Rt" ' + rowspanStr + '>' + hisData[i].lotteryDrawTime + '</td>' +
            '<td class="' + clsName1 + '" ' + rowspanStr + '>' + kjCommonFun.kjCheckNull(numberCode[0]) + '</td>' +
            '<td class="' + clsName1 + '" ' + rowspanStr + '>' + kjCommonFun.kjCheckNull(numberCode[1]) + '</td>' +
            '<td class="' + clsName1 + '" ' + rowspanStr + '>' + kjCommonFun.kjCheckNull(numberCode[2]) + '</td>' +
            '<td class="' + clsName1 + '" ' + rowspanStr + '>' + kjCommonFun.kjCheckNull(numberCode[3]) + '</td>' +
            '<td class="' + clsName1 + '" ' + rowspanStr + '>' + kjCommonFun.kjCheckNull(numberCode[4]) + '</td>' +
            '<td class="' + clsName1 + '" ' + rowspanStr + '>' + kjCommonFun.kjCheckNull(numberCode[5]) + '</td>' +
            '<td class="' + clsName2 + '" ' + rowspanStr + '>' + kjCommonFun.kjCheckNull(numberCode[6]) + '</td>'

            +
            '<td style="height:41px">' + kjCommonFun.kjCheckNull(lev1.piece) + '</td>' +
            '<td class="u-txar lineb2Rt">' + kjCommonFun.kjCheckNull(lev1.money) + '</td>' +
            '<td class="u-txar">' + kjCommonFun.kjCheckNull(lev2.piece) + '</td>' +
            '<td class="u-txar lineb2Rt">' + kjCommonFun.kjCheckNull(lev2.money) + '</td>' +
            '<td class="u-txar">' + kjCommonFun.kjCheckNull(lev3.piece) + '</td>' +
            ' <td class="lineb2Rt">' + kjCommonFun.kjCheckNull(lev3.money) + '</td>' +
            '<td class="u-txar">' + kjCommonFun.kjCheckNull(lev4.piece) + '</td>' +
            '<td class="lineb2Rt">' + kjCommonFun.kjCheckNull(lev4.money) + '</td>' +
            '<td class="u-txar">' + kjCommonFun.kjCheckNull(lev5.piece) + '</td>' +
            '<td class="lineb2Rt">' + kjCommonFun.kjCheckNull(lev5.money) + '</td>' +
            '<td class="u-txar">' + kjCommonFun.kjCheckNull(lev6.piece) + '</td>' +
            '<td class="lineb2Rt">' + kjCommonFun.kjCheckNull(lev6.money) + '</td>'

            +
            '<td class="u-txar" ' + rowspanStr + '>' + kjCommonFun.kjCheckNullStr(hisData[i].totalSaleAmount) + '</td>' +
            '<td class="u-txar" ' + rowspanStr + '>' + kjCommonFun.kjCheckNullStr(hisData[i].poolBalanceAfterdraw) + '</td>' +
            '<td ' + rowspanStr + '>' + kjCommonFun.linkGetDescript(hisData[i].lotteryDrawNum, hisData[i].pdfType, hisData[i].drawPdfUrl) + '</td>' +
            '</tr>';

        if (flag == 1) {
            dataStr += '<tr><td colspan="2" class="bgecf5fe lineb2Rt">派奖</td><td colspan="2" class="bgecf5fe lineb2Rt">派奖</td>' +
                '<td colspan="2" class="bgecf5fe lineb2Rt">派奖</td><td colspan="2" class="bgecf5fe lineb2Rt">派奖</td>' +
                '<td colspan="2" class="bgecf5fe lineb2Rt">派奖</td><td colspan="2" class="bgecf5fe lineb2Rt">派奖</td></tr>'+
                '<tr>'+
                lev1Str +
                lev2Str +
                lev3Str +
                lev4Str +
                lev5Str +
                lev6Str +
                '</tr>';
        }

    }
    $("#historyData").html(dataStr);
    kjCommonFun.allPage = data.pages;
    kjCommonFun.curPage = kjCommonFun.nextPage;
    if (kjCommonFun.allPage > 1) {
        //显示分页
        $('.m-page').html(kjCommonFun.pageListType(kjCommonFun.allPage, kjCommonFun.curPage));
    } else {
        $('.m-page').html('');
    }
    kjCommonFun.getIframeHeight($('.g-bd').height());
}
document.addEventListener('DOMContentLoaded', () => {
    window.addEventListener('message', (event) => {
        if (event.origin !== 'https://www.lottery.gov.cn' && event.origin !== 'https://www.sporttery.cn') {
            console.warn('消息来源不安全，忽略消息');
            return;
        }
        var eventData = event.data
        if(eventData['X-Dev-FP'] !=undefined){
            commonV1Fun.setCookie('X-Dev-FP', eventData['X-Dev-FP'])
            localStorage.setItem('X-Dev-FP', eventData['X-Dev-FP'])
        }
        if(eventData['X-Sensors-ID'] !=undefined){
            commonV1Fun.setCookie('X-Sensors-ID', eventData['X-Sensors-ID'])
            localStorage.setItem('X-Sensors-ID', eventData['X-Sensors-ID'])
        }
    });
});